//
//  SelectPhotoController.m
//  3DModelCollector
//
//  Created by Jing Zhang on 7/20/12.
//  Copyright (c) 2012 Tencent. All rights reserved.
//

#import "SelectPhotoController.h"
#import "PhotoInfo.h"

@interface SelectPhotoController ()

@property (retain, nonatomic) IBOutlet UIImageView *photoView;
@property (retain, nonatomic) IBOutlet UIScrollView *photoContainer;
@property (retain, nonatomic) IBOutlet UIButton *usePhotoButton;

@property (nonatomic) int curIndex;

- (IBAction)onClickCancel:(id)sender;
- (IBAction)onClickUsePhoto:(id)sender;
- (void)onTapPhoto:(UIGestureRecognizer*)gestureRecognizer;
- (void)updatePhotoInfo:(NSArray *)photoList;
- (void)selectOnePhoto:(int)index;

@end

@implementation SelectPhotoController

@synthesize photoInfoList = _photoInfoList;
@synthesize photoDelegate = _photoDelegate;
@synthesize curIndex = _curIndex;

@synthesize photoView;
@synthesize photoContainer;
@synthesize usePhotoButton = _usePhotoButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc {
    self.photoInfoList = nil;
    self.photoDelegate = nil;
    [photoView release];
    [photoContainer release];
    [_usePhotoButton release];
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self updatePhotoInfo:self.photoInfoList];
    if (self.photoInfoList.count > 0) {
        [self selectOnePhoto:0];
        self.usePhotoButton.enabled = YES;
    }
    else {
        self.usePhotoButton.enabled = NO;
    }
}

- (void)viewDidUnload
{
    [self setPhotoView:nil];
    [self setPhotoContainer:nil];
    [self setUsePhotoButton:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - [Private methods]

- (IBAction)onClickCancel:(id)sender {
    
    // 说明是直接从建筑详情页present而来
    if (self.navigationController == nil) {
        [self dismissModalViewControllerAnimated:YES];
    }
    // 说明是从拍照页面push而来
    else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (IBAction)onClickUsePhoto:(id)sender {
    
}

- (void)updatePhotoInfo:(NSArray *)photoList {
    
    int photoNum = photoList.count;
    photoContainer.contentSize = CGSizeMake((photoNum) * 80, 60);
    
    for (int i = 0; i < photoNum; i++) {
        PhotoInfo* onePhoto = [self.photoInfoList objectAtIndex:i];
        NSString* fullPhotoFileName = [NSString stringWithFormat:@"%@/%@.jpg", @"", onePhoto.name];
        UIImage* image = [UIImage imageWithContentsOfFile:fullPhotoFileName];
        if (image == nil) {
            continue;
        }
        
        CGRect thumbRect;
        if (image.size.width < image.size.height) {
            thumbRect.size.height = 60;
            thumbRect.size.width = image.size.width * 60.0 / image.size.height;
            thumbRect.origin.x = 80 * i + (80 - thumbRect.size.width) / 2;
            thumbRect.origin.y = 4;
        }
        else {
            thumbRect.size.width = 60;
            thumbRect.size.height = image.size.height * 60.0 / image.size.width;
            thumbRect.origin.x = 80 * i + 10;
            thumbRect.origin.y = (60 - thumbRect.size.height) / 2;
        }
        
        UIImageView* newImageView = [[UIImageView alloc] initWithFrame:thumbRect];
        newImageView.userInteractionEnabled = YES;
        // To do 2 : 可以考虑压缩成略缩图，以节省内存
        newImageView.image = image;
        
        // 添加点击事件响应
        UITapGestureRecognizer* tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapPhoto:)];
        [newImageView addGestureRecognizer:tapGestureRecognizer];
        [tapGestureRecognizer release];
        
        // 添加至滚动视图
        [self.photoContainer addSubview:newImageView];
        [newImageView release];
    }
}

- (void)onTapPhoto:(UIGestureRecognizer*)gestureRecognizer {
    
    UIView* onePhotoView = gestureRecognizer.view;
    CGRect frame = onePhotoView.frame;
    
    int index = frame.origin.x / 80;
    self.curIndex = index;
    
    [self selectOnePhoto:self.curIndex];
}

- (void)selectOnePhoto:(int)index {
    
    if (index < 0 || index >= self.photoInfoList.count) {
        return;
    }
    
    // ThumbView background view
    UIView* bgImage = [self.photoContainer viewWithTag:10];
    
    [UIView animateWithDuration:0.3f animations:^() {
        CGRect frame = bgImage.frame;
        frame.origin.x = index * 80 + 10;
        bgImage.frame = frame;
    }];
    
    // Big image view
    PhotoInfo* onePhoto = [self.photoInfoList objectAtIndex:index];
    NSString* fullPhotoFileName = [NSString stringWithFormat:@"%@/%@.jpg", @"", onePhoto.name];
    UIImage* image = [UIImage imageWithContentsOfFile:fullPhotoFileName];
    if (image == nil) {
        return;
    }
    
    CGRect thumbRect;
    if (image.size.width < image.size.height) {
        thumbRect.size.height = 320;
        thumbRect.size.width = image.size.width * 320.0 / image.size.height;
        thumbRect.origin.x = (320 - thumbRect.size.width) / 2;
        thumbRect.origin.y = 0;
    }
    else {
        thumbRect.size.width = 320;
        thumbRect.size.height = image.size.height * 320.0 / image.size.width;
        thumbRect.origin.x = 0;
        thumbRect.origin.y = (320 - thumbRect.size.height) / 2;
    }
    
    self.photoView.frame = thumbRect;
    self.photoView.image = image;
}

@end
